<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class article_controller extends admin_controller
{
    // Get list
    public function __construct()
    {
        parent::__construct();
        $this->page_title = 'Thông tin';
        $this->table_name = 'article';
        $this->route_name = 'article';
        $this->business_model = $this->article_model;
    }

    public function get_post_data()
    {
        $image_path = $this->upload_image('new_image',$this->input->post('old_image'));
        $post_data = array(
            'title' => $this->input->post('title'),
            'short_content' => $this->input->post('short_content'),
            'content' => $this->input->post('content'),
            'url' => $this->input->post('url'),
            'image_path' => $image_path,
            'modified_date' => time()
        );
        return $post_data;
    }

    public function get_list()
    {
        $list = array();
        $news = $this->business_model->get_elements();
        foreach($news as $item)
        {
            $actions = array();
            $actions[] = array(
                'text' => 'Sửa',
                'href' => base_url(). $this->base_admin_url .$this->route_name.'/update/'.$item->id
            );
            $list[] = array(
                'can_delete' => $item->id,
                'title' => $item->title,
                'short_content' => substr_words($item->short_content,30),
                'content' => substr_words($item->content,100),
                'url' => $item->url,
                'image_path' => $item->image_path,
                'actions' => $actions
            );
        }
        return $list;
    }

    public function get_header_list()
    {
        $list = array();
        $list[] = 'Tiêu đề';
        $list[] = 'Tóm tắt';
        $list[] = 'Nội dung';
        $list[] = 'Đường dẫn';
        $list[] = 'Hình ảnh';
        $list[] = 'Công việc';
        return $list;
    }

    public function get_fields($id)
    {
        if($id == -1)
        {
            $model->title = '';
            $model->short_content = '';
            $model->content = '';
            $model->url = '';
            $model->image_path = '';
        }
        else
        {
            $model = $this->business_model->get_element_by_id($id);
        }
        $fields = array();
        $fields[] = array(
            'text' => 'Tiêu đề',
            'name' => 'title',
            'value' => $model->title,
            'type' => 'text'
        );
        $fields[] = array(
            'text' => 'Tóm tắt',
            'name' => 'short_content',
            'value' => $model->short_content,
            'type' => 'textarea'
        );
        $fields[] = array(
            'text' => 'Nội dung',
            'name' => 'content',
            'value' => $model->content,
            'type' => 'textarea'
        );
        $fields[] = array(
            'text' => 'Đường dẫn',
            'name' => 'url',
            'value' => $model->url,
            'type' => 'text'
        );
        $fields[] = array(
            'text' => 'Hình ảnh cũ',
            'name' => 'old_image',
            'value' => $model->image_path,
            'type' => 'hidden'
        );
        $fields[] = array(
            'text' => 'Hình ảnh mới',
            'name' => 'new_image',
            'value' => '',
            'type' => 'file'
        );
        return $fields;
    }
}